//
//  TapSupportViewController.swift
//  kz
//
//  Created by Tech_001 on 14/8/2017.
//  Copyright © 2017 Tech_001. All rights reserved.
//

import Foundation
import UIKit

class TabSupportViewController: UIViewController, UIWebViewDelegate{

    var webView: UIWebView?
    var initUrl: String?
    
    var _loadView: UIView?
    var balanceButton: UIButton?
    
//    override func viewWillAppear(_ animated: Bool) {
//        super.viewWillAppear(animated)
//        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)) , name: NSNotification.Name.UIKeyboardWillShow, object: nil)
//        
//    }
//    func keyboardWillShow(_ notification: Notification) {
//        print("通知中心")
//        var userinfo:Dictionary = notification.userInfo!
//        let value  = userinfo.index(forKey: UIKeyboardFrameEndUserInfoKey)
//        
//    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if let maintainData = GlobalInfo.gameMaintainData {
            self.initUrl = maintainData["maintain_cs_url"]!
        }else{
            if let siteData = GlobalInfo.siteInfoData {
                if let supportURL = siteData["support"] as? String {
                    self.initUrl = supportURL
                }else {

                    self.initUrl = GlobalInfo.defaultCSUrl
                }
            }
            if let serviceIurl=GlobalInfo.serviceIurl {
                self.initUrl=serviceIurl
            }
         	
        }
        
        /**** Interface ****/
        let containerView = UIView()
        let vc: WebViewController = WebViewController.init()
        var originY: CGFloat = 64.5
        let minusHeight: CGFloat = 64
        
        vc.bottomOffset = 113
        if UIHelper.fullSize.height == ScreenSize.iPhoneX.height {
            originY += 23
            vc.bottomOffset += 58
        }
        
        containerView.frame = CGRect(x: 0, y: originY, width: self.view.frame.width, height: self.view.frame.height - minusHeight)
        
        vc.loadViewOffsetY = -64
        vc.url = self.initUrl
        vc.view.frame = CGRect(x: 0, y: 0, width: containerView.frame.size.width, height: containerView.frame.size.height)
        vc.didMove(toParent: self)
        self.webView = vc.webView
        
        self.addChild(vc)
        
        containerView.addSubview(vc.view)
        
        let rightItem = UIBarButtonItem(title: "刷新", style: .plain, target: self, action: #selector(self.buttonReloadClick))
        let leftItem = UIBarButtonItem(title: "刷新", style: .plain, target: self, action: #selector(self.buttonReinitClick))
        
        
        /**** Balance ****/
        let balanceButton = UIButton(type: .custom)
        balanceButton.setTitle("Fetchin", for: .normal)
        balanceButton.setImage(UIImage(named: "icon_nav_right_amount"), for: .normal)
        balanceButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: -12, bottom: 0, right: 0)
        balanceButton.setTitleColor(UIColor.white, for: .normal)
        balanceButton.titleLabel?.font = UIFont(name: balanceButton.titleLabel!.font.fontName, size: 14)
        self.balanceButton = balanceButton
        
        let barItem = UIBarButtonItem(customView: balanceButton)
        
        self.navigationItem.setRightBarButton(barItem, animated: true)
        balanceButton.addTarget(self, action: #selector(self.pushWebViewTransfer), for: .touchUpInside)
        /*** Balance ***/
        
        
        let topLogo = UIImage(named: "icon_nav_left_logo")
        let titleView = UIView()
        let topLogoView = UIImageView(image: topLogo)
        titleView.addSubview(topLogoView)
        topLogoView.contentMode = .scaleToFill
        topLogoView.frame = CGRect(x: -(137/2), y:-(35/2), width: 137, height: 35)
        navigationItem.titleView = titleView
        navigationItem.leftBarButtonItem = leftItem
      //  navigationItem.rightBarButtonItem = rightItem
        self.navigationController?.navigationBar.tintColor = UIColor.black
        refreshBalance()
        //self.view.addSubview(navigationBar)
        self.view.addSubview(containerView)
        /**** Interface ****/
    }
    func refreshBalance(){
        if UserHelper.getLoginStatus() {
            if let withBalance=GlobalInfo.memberInfoData!["balance"] as? String {
                var  balance: Double = Double(withBalance)!
                self.balanceButton?.setTitle(String(format: "%.2f", balance), for: .normal)
                let size = self.balanceButton!.intrinsicContentSize
                  balanceButton!.frame.size = CGSize(width: size.width, height: 33)
               
            }
        }else{
            let button1 = UIBarButtonItem(title: "注册/登录", style: .plain, target: self, action: #selector(self.loginRegisterButtonClick))
            button1.tintColor = UIColor.orange
            
            self.navigationItem.setRightBarButtonItems([button1], animated: false)
        }
    }
    @objc func pushWebViewTransfer(){
        if  GlobalInfo.isPlatformMainhtml == nil || GlobalInfo.isPlatformMainhtml != false{
            self.navigateWebView(toPath: "/walletm/transfer", "",sign: true)
        }else{
            let vc = MainAccountVC()
            vc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    /**** Others ****/
    func navigateWebView(toPath: String, _ title: String,sign:Bool){
        let goNext: WebViewController = WebViewController.init()
        let url = "https://" + UserDefaults.standard.string(forKey: DefaultKey.USER_DOMAIN)! + toPath
        goNext.url = url
        goNext.title = title
        goNext.signLoging=sign
        goNext.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(goNext, animated: true)
    }
    /**** View Actions ****/
    @objc func loginRegisterButtonClick(){
         setRootLoginVC()
       // GlobalInfo.mainTabBarController?.navigationController?.popToRootViewController(animated: true)
        //        self.dismiss(animated: true, completion: nil)
    }
    func webViewDidStartLoad(_ webView: UIWebView) {
        if self._loadView == nil {
            self._loadView = UIHelper.initLoadView(self)
        }
        self.view.addSubview(self._loadView!)
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView) {
        self._loadView?.removeFromSuperview()
    }
    
    func initWebView(_ url: String){
        self.initUrl = url
        if let urlObj = URL(string: url){
            var request = URLRequest(url: urlObj)
            request.httpShouldHandleCookies = true
            webView?.loadRequest(request)
        }
    }
    
    @objc func buttonReloadClick() {
        webView?.reload()
    }

    @objc func buttonReinitClick() {
        initWebView(self.initUrl!)
    }

}
